﻿using System;
using System.Collections.Generic;
using System.Text;
using Castle.Services.Transaction;
using MiragePWF.NH.Logging;
using MiragePWF.NH.Repositories;
using MiragePWF.NH.UOW;
using NHibernate.Criterion;
using NHibernate.SqlCommand;
using Wolfut.MirageEE.Domain.Common;
using Wolfut.MirageEE.Interface.Common;
using Wolfut.MirageEE.Kernal.Common;


namespace Wolfut.MirageEE.Server.Server.Common
{
    [Transactional]
    [AutoLogging]
    public class DataDictionaryService : BusinessServiceBase,IDataDictionaryService
    {
        private readonly IRepository<DataDictionary> dictionaryRepository;
        private static IList<DataDictionary> listDictionary = null;
        public DataDictionaryService(IRepository<DataDictionary> dictionaryRepository)
        {
            this.dictionaryRepository = dictionaryRepository;
        }

        #region IDataDictionaryService Member
        [Transaction(TransactionMode.Requires)]
        public DataDictionary AddOrUpdateDataDictionary(DataDictionary item)
        {
            dictionaryRepository.Save(item);

            #region Clear Cache Item
            //Clear Cache Item
            if (listDictionary != null)
            {
                listDictionary.Clear();
                listDictionary = null;
            }
            #endregion

            return item;
        }

        [Transaction(TransactionMode.Requires)]
        public void DeleteDataDictionary(DataDictionary item)
        {
            if (item == null)
            {
                return;
            }
            dictionaryRepository.Delete(item);
            if (listDictionary != null)
            {
                listDictionary.Clear();
                listDictionary = null;
            }
        }

        /// <summary>
        /// Caching DataDictionaryList.....
        /// </summary>
        /// <param name="isDeleted"></param>
        /// <returns></returns>
        public IList<DataDictionary> ListDataDictionary(Boolean isDeleted)
        {
            if (listDictionary == null)
            {
                DetachedCriteria criteria = DetachedCriteria.For(typeof(DataDictionary));
                criteria.Add(Expression.Eq("IsDeleted", isDeleted));
                listDictionary = this.dictionaryRepository.FindAll(criteria);
            }
            return listDictionary;            
        }
        public IList<DataDictionary> listDataDictionary(Boolean isDeleted, String category)
        {
            IList<DataDictionary> lists = this.ListDataDictionary(isDeleted);
            IList<DataDictionary> returnLists = new List<DataDictionary>();
            
            if ((lists ==null)||(lists.Count == 0))
            {
                return null;
            }
            foreach (DataDictionary item in lists)
            {
                if (item.Category == category)
                {
                    returnLists.Add(item);
                }
            }
            return returnLists;
        }
        public DataDictionary GetDataDictionary(String code)
        {
            IList<DataDictionary> lists = this.ListDataDictionary(true);
            foreach (DataDictionary item in lists)
            {
                if (item.Code == code)
                {
                    return item;
                    //break;
                }
            }
            return null;
        }
        #endregion
    }
}
